package com.yishan.controller;

import com.github.pagehelper.PageInfo;
import com.yishan.domain.Dept;
import com.yishan.domain.Draw;
import com.yishan.domain.Type;
import com.yishan.domain.User;
import com.yishan.service.DrawService;
import com.yishan.service.TypeService;
import com.yishan.service.UserService;
import com.yishan.utils.BeanUtil;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author yishan
 * @Date 2021/4/28 0028 16:16
 * @Version 1.0
 */
@Controller
@RequestMapping("/draw")
public class DrawController {

	@Autowired
	private DrawService drawService;

	@Autowired
	private TypeService typeService;

	@Autowired
	private UserService userService;

	/**
	 * 查询所有
	 *
	 * @param model
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/list")
	private String list(Model model, HttpServletRequest request)
			throws ServletException, IOException {

		int page = 1;
		int size = 8;

		if (StringUtils.isNotBlank(request.getParameter("page"))) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (StringUtils.isNotBlank(request.getParameter("size"))) {
			size = Integer.parseInt(request.getParameter("size"));
		}

		PageInfo info = drawService.findAll(page, size);

		model.addAttribute("page", info);

		return "/WEB-INF/pages/draw/list.jsp";

	}

	/**
	 * 跳转到添加页面
	 *
	 * @return
	 */
	@RequestMapping("/toAdd")
	private String toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//修改需要查询出类别的信息
		List<Type> typeList = typeService.findAll();
		for (Type type : typeList) {
			System.out.println(type);
		}

		request.setAttribute("typeList", typeList);

		List<User> userList = userService.findAll();
		request.setAttribute("userList", userList);

		request.getRequestDispatcher("/WEB-INF/pages/draw/add.jsp").forward(request, response);

		return "/WEB-INF/pages/draw/add.jsp";

	}

	/**
	 * 添加操作
	 *
	 * @param request
	 * @return
	 */
	@PostMapping("/save")
	private String save(HttpServletRequest request, @RequestParam("durl") MultipartFile file) throws Exception {
		Draw draw = BeanUtil.fillBean(request, Draw.class);
		if (ObjectUtils.isEmpty(draw)) {
			return null;
		}
		if (ObjectUtils.isEmpty(file)) {
			return null;
		}
		draw.setDraw_name(file.getOriginalFilename());

		String realPath = request.getServletContext().getRealPath("/pdfjs/web");
		String url = UUID.randomUUID().toString().replace("-", "").substring(0, 15) + ".pdf";
		if (!StringUtils.isNotEmpty(url)) {
			return null;
		}
		file.transferTo(new File(realPath, url));
		draw.setDurl(url);
		drawService.save(draw);
		return "redirect:" + request.getContextPath() + "/draw/list";

	}

	/**
	 * 跳转到修改页面
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEdit")
	private String toEdit(String id, Model model) {

		Draw draw = drawService.findById(id);

		model.addAttribute("draw", draw);

		//修改需要 查询出分类的信息
		List<Type> typeList = typeService.findAll();
		model.addAttribute("typeList", typeList);

		//修改需要 查询出负责人信息
		List<User> userList = userService.findAll();
		model.addAttribute("userList", userList);
		return "/WEB-INF/pages/draw/update.jsp";

	}

	/**
	 * 修改操作
	 *
	 * @param draw
	 * @return
	 */
	@RequestMapping("/edit")
	private String edit(Draw draw) {

		drawService.update(draw);

		return "redirect:/draw/list";

	}

	/**
	 * 删除操作
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	private String delete(String ids) {
		System.out.println(ids);

		drawService.deleteMany(ids);

		return "redirect:/draw/list";

	}

	/**
	 * 预览
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/toOpenPdf")
	private String toOpenPdf(String id) {

		Draw draw = drawService.findById(id);
		String file = draw.getDurl();
		return "redirect:/pdfjs/web/viewer.html?file=" + file;
	}


	/**
	 * 分类查询
	 *
	 * @param id
	 * @param model
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/toList")
	private String toList(String id, Model model, HttpServletRequest request)
			throws ServletException, IOException {

		int page = 1;
		int size = 8;

		if (StringUtils.isNotBlank(request.getParameter("page"))) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (StringUtils.isNotBlank(request.getParameter("size"))) {
			size = Integer.parseInt(request.getParameter("size"));
		}

		PageInfo info = drawService.findTypeId(page, size, id);

		model.addAttribute("page", info);
		model.addAttribute("typeId",id);
		return "/WEB-INF/pages/type/toList.jsp";

	}
}
